package com.zf.shuashua.ui.mine;

import android.content.Intent;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.zf.shuashua.R;
import com.zf.shuashua.base.BaseActivity1;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class FocusListActivity extends BaseActivity1 implements ListenerList<Integer> {
    private Toolbar mToolbar;
    private TextView mTvTitle;
    private String[] titles = {"我的关注", "粉丝"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private MyPagerAdapter adapter;
    private ViewPager vp;
    private SlidingTabLayout tabLayout;
    private FragmentManager mf;
    private List<String> tagList;
    private Intent intent;

    @Override
    protected void initView() {
        intent = getIntent();
        if (intent.getStringExtra("USER_ID") != null) {
            titles = new String[]{"Ta的关注", "Ta粉丝"};
        }
        mToolbar = findViewById(R.id.toolbar);
        mTvTitle = findViewById(R.id.tv_title);
        initToolBar(mToolbar, true, "");
        mTvTitle.setText("关注列表");
        fragments.add(FocusFragment.newInstance(intent.getIntExtra("FocusList1", 0), intent.getStringExtra("USER_ID")));
        fragments.add(FansFragment.newInstance(intent.getIntExtra("FocusList", 0), intent.getStringExtra("USER_ID")));
        tabLayout = findViewById(R.id.tl);
        tagList = new ArrayList<>();
        vp = findViewById(R.id.vp);
        mf = getSupportFragmentManager();
        adapter = new MyPagerAdapter(mf);
        vp.setAdapter(adapter);
        tabLayout.setViewPager(vp, titles);
        tabLayout.setCurrentTab(intent.getIntExtra("FocusList", 0));
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }


//        public Object instantiateItem(ViewGroup container, int position) {
//
//            tagList.add(makeFragmentName(container.getId(), (int) getItemId(position)));
//
//            return super.instantiateItem(container, position);
//        }

        //暂时这样
//        public void update(int item) {
//            Fragment fragment = mf.findFragmentByTag(tagList.get(item));
//            if (fragment != null) {
//                if (getIntent().getIntExtra("FocusList", 0) == 0) {
//                    switch (item) {
//                        case 0:
//                            ((FocusFragment) fragment).update();
//                            break;
//                        case 1:
//                            ((FansFragment) fragment).update();
//                            break;
//                        default:
//                            break;
//                    }
//                } else if (getIntent().getIntExtra("FocusList", 0) == 1) {
//                    switch (item) {
//                        case 0:
//                            ((FansFragment) fragment).update();
//                            break;
//                        case 1:
//                            ((FocusFragment) fragment).update();
//                            break;
//                        default:
//                            break;
//                    }
//
//                }
//            }
//
//        }

    }

    @Override
    public void listener(Integer list) {
//        adapter.update(list);
    }

//    public static String makeFragmentName(int viewId, int index) {
//        return "android:switcher:" + viewId + ":" + index;
//    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_focus_list;
    }

}
